#!/bin/sh

JAR_NAME=$1
PROJECT_VERSION=$2
PORT=$3

export IMAGE_VERSION=$PROJECT_VERSION

# 1.创建dockerfile
cat > Dockerfile <<EOF

FROM fabric8/java-centos-openjdk8-jre:latest

ADD $JAR_NAME.$PROJECT_VERSION.jar /app/

# 应用需要暴露的端口
EXPOSE ${PORT}

WORKDIR /app

CMD ["java", "-jar" ,"-Duser.timezone=GMT+08","-XX:MetaspaceSize=10m","-XX:MaxMetaspaceSize=50m","-Xms200m","-Xmx200m","-Xss256k","-XX:SurvivorRatio=8","-XX:+UseConcMarkSweepGC","$JAR_NAME.$PROJECT_VERSION.jar"]
#CMD ["java", "-jar" ,"-XX:ErrorFile=/var/log/skywalkingdemo_error_%p.log","-Duser.timezone=GMT+08","-XX:MetaspaceSize=128m","-XX:MaxMetaspaceSize=128m","-Xms1024m","-Xmx2048m","-Xmn256m","-Xss256k","-XX:SurvivorRatio=8","-XX:+UseConcMarkSweepGC","$JAR_NAME.$PROJECT_VERSION.jar"]

EOF

pwd

#2. 构建docker镜像
BUILD_NUMBER=${BUILD_NUMBER:-"1"}
BUILD_DATETIME=$(date "+%Y%m%d.%H%M%S")
IMAGE_VERSION=$IMAGE_VERSION-$BUILD_DATETIME-$BUILD_NUMBER
export IMAGE_VERSION

docker build -t $JAR_NAME:$IMAGE_VERSION .

#推送
#docker push "$REGISTRY/$IMAGE_NAME:$IMAGE_VERSION" && docker rmi -f "$REGISTRY/$IMAGE_NAME:$IMAGE_VERSION"
